<%@ page import="java.sql.Connection" %>
<%@ page import="java.util.List" %>
<%@ page import="edu.sctu.User" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.DriverManager" %><%--
  Created by IntelliJ IDEA.
  User: LENOVO
  Date: 2018/6/24
  Time: 0:50
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>检验</title>
</head>
<body>
<%
    String username = request.getParameter("username");
    String password = request.getParameter("password");


    //数据库中读取数据保存在List集合中
    List<User> userList=new ArrayList<User>();

    //3+1读取数据库数据保存到List中
    Connection connection=null;
    Statement statement=null;
    ResultSet resultSet=null;

    String sql="select * from t_users";

    Class.forName("com.mysql.jdbc.Driver");


    connection= DriverManager.getConnection(
            "jdbc:mysql://localhost:3306/sctu_javaweb_2018?serverTimezone=UTC",
            "root",
            "123456"
    );

    statement=connection.createStatement();
    resultSet=statement.executeQuery(sql);

    while(resultSet.next()){
        int id=resultSet.getInt(1);
        String name=resultSet.getString(2);
        String password0=resultSet.getString(3);

        User user = new User();

        user.setId(id);
        user.setName(name);
        user.setPassword(password0);

        userList.add(user);
    }


    resultSet.close();
    statement.close();
    connection.close();

    request.setAttribute("userList", userList);

    for(int i = 0; i<userList.size(); i++) {
        if(userList.get(i).getName().equals(username) && userList.get(i).getPassword().equals(password)) {
            out.write("登录成功!");
            boolean isLogin = true;
            session.setAttribute("isLogin", "true");
            response.sendRedirect("userList.jsp");
        }
    }
    out.write("<p>用户名或密码错误。</p>");
    out.write("<meta http-equiv=\"refresh\" content=\"1;URL=http://localhost:8080/login.jsp\">");
%>
</body>
</html>
